<template>
  <!-- 图书馆离校管理 -->
  <div class="app-container">
    <div class="title-tips" v-if="timeHorizon.startTime && timeHorizon.endTime">
      温馨提示：毕业离校时间为{{ timeHorizon.startTime }}-{{
        timeHorizon.endTime
      }}，请在该时间内进行离校操作
    </div>
    <div class="library-title">
      <div class="contens">毕业离校学生：{{ bookForm.NUMBERTOTAL }}人</div>
      <div class="contens">未还书的学生：{{ bookForm.COMPNUMBERTOTAL }}人</div>
      <div class="contens">未还书目：{{ bookForm.TSSLCOUNT }}本</div>
      <div class="contens">赔偿金额：{{ bookForm.SUMCOMPNUMBER }}元</div>
    </div>

    <el-row>
      <el-form
        :model="queryParams"
        label-width="90px"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
      >
        <el-col :span="6">
          <el-form-item label="毕业年份">
            <el-select
              v-model="queryParams.dgsId"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in yearList"
                :key="item.DGS_ID"
                :label="item.GRAD_YEAR"
                :value="item.DGS_ID"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="学位">
            <el-select
              v-model="queryParams.edLevel"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="dict in dict.type.basic_speciality_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="毕业生状态">
            <el-select
              v-model="queryParams.gdpStatus"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="dict in dict.type.dep_grad_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="借还状态">
            <el-select
              v-model="queryParams.barStatus"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="dict in dict.type.bar_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="院系">
            <el-select
              v-model="queryParams.deptId"
              clearable
              placeholder="请选择"
              @change="handSpeciality"
            >
              <el-option
                v-for="dict in classTreeList"
                :key="dict.id"
                :label="dict.label"
                :value="dict.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="专业">
            <el-select
              v-model="queryParams.specialityId"
              clearable
              placeholder="请选择"
              @change="changeGrade"
            >
              <el-option
                v-for="dict in specialityList"
                :key="dict.VALUE"
                :label="dict.TITILE"
                :value="dict.VALUE"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item prop="gradeId" label="年级">
            <el-select
              v-model="queryParams.gradeId"
              placeholder="请选择"
              @change="changeGrade"
              clearable
              class="elSelect"
            >
              <el-option
                v-for="item in gradeDate"
                :key="item.VALUE"
                :label="item.TITILE"
                :value="item.VALUE"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="班级">
            <el-select
              v-model="queryParams.classId"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="dict in classListType"
                :key="dict.VALUE"
                :label="dict.TITLE"
                :value="dict.VALUE"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="学生注册号">
            <el-input
              v-model="queryParams.regCode"
              placeholder="请输入注册号"
              clearable
            />
          </el-form-item>
        </el-col>

        <el-col :span="6" style="float: right; text-align: right">
          <el-form-item>
            <div>
              <el-button
                type="primary"
                icon="el-icon-search"
                @click="onSearch"
                size="mini"
                >搜索</el-button
              >
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
                >重置</el-button
              >
            </div>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <div style="float: right">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['departure:bookGradStuRoster:export']"
            >导出数据</el-button
          >
        </el-col>
      </el-row>
    </div>
    <el-table v-loading="loading" :data="gradStuRosterList">
      <el-table-column
        label="学院专业班级"
        align="center"
        show-overflow-tooltip
        prop="dsc"
      >
      </el-table-column>
      <el-table-column label="注册号" align="center" prop="regCode">
      </el-table-column>
      <el-table-column label="学生姓名" align="center" prop="studentName" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="未还书本" align="center" prop="tssl" show-overflow-tooltip>
      </el-table-column>
      <el-table-column label="学位" align="center" prop="edLevel" width="100" show-overflow-tooltip>
      </el-table-column>
      <el-table-column
        label="毕业生状态"
        align="center"
        prop="gdpStatus"
        width="100"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.dep_grad_status"
            :value="scope.row.gdpStatus"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="毕业离校确认时间"
        align="center"
        prop="gdpTime"
        width="180"
      >
      </el-table-column>
      <el-table-column
        label="赔偿金"
        align="center"
        prop="compAmount"
        width="180"
      >
      </el-table-column>
      <el-table-column label="书名" align="center" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handlebook(scope.row)"
            >查看书名</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleEdit(scope.row)"
            v-hasPermi="['departure:bookGradStuRoster:edit']"
            :disabled="operate"
            >修改赔偿金</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 修改操作 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" label-width="80px" :rules="rules">
        <el-form-item label="赔偿金" prop="compAmount">
          <el-input
            v-model.trim="form.compAmount"
            placeholder="请输入赔偿金"
            type="number"
            @input="limitInput($event, 'compAmount')"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 书名 -->
    <el-dialog
      :title="title"
      :visible.sync="bookOpen"
      width="30%"
      append-to-body
    >
      <div class="book-title" v-for="(item, index) in bookTitle" :key="index">
        <div>《{{ item.title }}》</div>
        <div>书费：{{ item.price }}元</div>
      </div>
    </el-dialog>
  </div>
</template>
      
      <script>
import {
  bookGradStuRosterList,
  updataBookGradStuRoster,
  bookGradStuRosterStat,
  lookBook,
} from "@/api/departure/librarys";
import {
  getGradYearSelector,
  getSettlementTime,
} from "@/api/departure/gradsettings";
import searchForm from "@/components/Search/searchForm.vue";
import pieChart from "@/views/components/pieChart/index";
import { getClassSelect } from "@/api/information/classInfor";
import { classTree } from "@/api/information/studentInfor";
import { getSpecialityByDeptId } from "@/api/information/unitMajor";
import { getGradeSelect } from "@/api/information/gradeInfor";
import moment from "moment";

export default {
  name: "library",
  dicts: ["dep_grad_status", "bar_status", "basic_speciality_type"],
  components: { searchForm, pieChart },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      yearStr: "",
      // 毕业离校学生表格数据
      gradStuRosterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      bookOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        specialityId: undefined,
        gdpStatus: null,
        classId: undefined
      },
      rules: {
        compAmount: [
          { required: true, message: "请输入赔偿金", trigger: "bulr" },
        ],
      },
      // 表单参数
      form: {},
      timeHorizon: {},
      yearList: [],
      classListType: [],
      classTreeList: [],
      specialityList: [],
      gradeDate: [],
      timeForm: {},
      operate: true,
      bookTitle: [],
      bookForm: {},
    };
  },
  created() {
    getGradYearSelector().then((res) => {
      this.yearList = res.data;
      this.queryParams.dgsId = res.data[0].DGS_ID;

      classTree().then((response) => {
        this.classTreeList = response.data;
      });
      this.getList();
      this.getTitme(this.queryParams.dgsId)
    });
  },
  methods: {
    getTitme(data) {
      if(data){
        getSettlementTime(data).then((res) => {
      console.log(res, "时间");
      if (res.code == 200) {
        this.timeHorizon = res.data
        if (
          new Date().getTime() >= new Date(this.timeHorizon.startTime).getTime() &&
          new Date().getTime() <= new Date(this.timeHorizon.endTime).getTime()
        ) {
          this.operate = false;
        } else {
          this.operate = true;
        }
      } else {
        this.operate = true;
      }
    });
      }
      
    
    },
    // 获取搜索表单提交的数据
    onSearch() {
      // 查询条件和分页数据合并
      this.getList();
      this.getTitme(this.queryParams.dgsId)

    },
    // 重置
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
         dgsId: null,
        specialityId: undefined,
        gdpStatus: null,
        classId: undefined
      };
      this.queryParams.dgsId = this.yearList[0].DGS_ID;
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.classListType = []
      this.gradeDate = []
      this.specialityList = []
      this.onSearch();
    },

    handlebook(row) {
      this.title = "书名";
      this.bookOpen = true;
      lookBook(row.studentId).then((res) => {
        console.log(res, "书名");
        this.bookTitle = res.data;
      });
    },
    getClassList() {
      getClassSelect({
        gradeId: this.queryParams.gradeId,
        specialtyId: this.queryParams.specialityId,
      }).then((res) => {
        console.log(res, "班级");
        this.classListType = res.data;
      });
    },
    // 年级
    getGradeInfor() {
      getGradeSelect().then((response) => {
        this.gradeDate = response.data;
      });
    },
    changeGrade(value) {
      console.log(value, "ceeee");
      if (value != undefined) {
        if (this.queryParams.specialityId && this.queryParams.gradeId) {
          this.getClassList();
          this.classListType = [];
          this.queryParams.classId = "";
        } else {
          if (!value) {
            this.classListType = [];
            this.queryParams.classId = "";
          }
        }
      }
    },
    async handSpeciality() {
      this.queryParams.specialityId = ''
      this.queryParams.classId = ''
      this.classListType = [];
      this.getSpeciality() 
      this.getGradeInfor()
    },
    // 专业
    async getSpeciality() {
      if (this.queryParams.deptId) {
        await getSpecialityByDeptId({ deptId: this.queryParams.deptId }).then(res => {
          if (res.code == "200") {
            this.specialityList = res.data;
          } else {
            this.specialityList = [];
          }
        });
      } else {
        this.specialityList = [];
      }
    },
    /** 查询毕业离校学生列表 */
    getList() {
      this.loading = true;
      bookGradStuRosterList(this.queryParams).then((response) => {
        this.gradStuRosterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      bookGradStuRosterStat({ dgsId: this.queryParams.dgsId }).then((res) => {
        console.log(res, "tomgji");
        this.bookForm = res.data[0];
      });
    },
    // 新增离校学生操作按钮
    handleAdd() {
      this.open = true;
      this.title = "新增";
    },
    // 修改退费操作按钮
    handleEdit(row) {
      this.open = true;
      this.title = "修改赔偿金";
      this.form = JSON.parse(JSON.stringify(row));
    },
    //限制只能输入两位小数
    limitInput(value, name) {
      this.form[name] =
        ("" + value) // 第一步：转成字符串
          .replace(/[^\d^\.]+/g, "") // 第二步：把不是数字，不是小数点的过滤掉
          .replace(/^0+(\d)/, "$1") // 第三步：第一位0开头，0后面为数字，则过滤掉，取后面的数字
          .replace(/^\./, "0.") // 第四步：如果输入的第一位为小数点，则替换成 0. 实现自动补全
          .match(/^\d*(\.?\d{0,2})/g)[0] || ""; // 第五步：最终匹配得到结果 以数字开头，只有一个小数点，而且小数点后面只能有0到2位小数
    },
    cancel() {
      this.regCode = "";
      this.open = false;
    },
    /** 提交按钮 */
    submitForm() {
      console.log(this.form, "compAmount");
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updataBookGradStuRoster({
            studentId: this.form.studentId,
            compAmount: this.form.compAmount,
            dgsrId: this.form.dgsrId,
            dgrdId: this.form.dgrdId

          }).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/departure/bookGradStuRoster/export",
        {
          ...this.queryParams,
        },
        `图书馆离校${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
      <style scoped>
.title-tips {
  text-align: center;
  border: 1px solid #d0d0d0;
  padding: 5px;
  margin-bottom: 5px;
}
.library-title {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
}
.contens {
  width: 24%;
  border: 1px solid #d0d0d0;
  text-align: center;
  padding: 2%;
}
.book-title {
  display: flex;
  justify-content: space-between;
}
::v-deep input::-webkit-outer-spin-button,
::v-deep input::-webkit-inner-spin-button {
  -webkit-appearance: none;
}
::v-deep input[type="number"] {
  -moz-appearance: textfield;
  appearance: none;
}
::v-deep inpit {
  border: none;
}
</style>